package com.android.tea.util;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

	/**
	 * 将byte数组转换为字符串,
	 * 
	 * @param array
	 *            byte数组
	 * @param length
	 *            截取的长度,从0开始,因中文字符等原因,实际截取的长度可能小于length
	 * @param charsetName
	 *            生成的字符串的字符编码
	 * @return
	 */
	public static String getStringFromByteArray(byte[] array, int length,
			String charsetName) {
		String str = "";
		int sig = 1;
		if (length <= array.length) {
			for (int i = 0; i < length; i++) {
				sig = array[i] * sig >= 0 ? 1 : -1;
			}
			if (sig < 0) {
				length -= 1;
			}
			try {
				str = new String(array, 0, length, charsetName);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return str;
	}

	public static boolean isNullOrEmpty(String str) {
		boolean flag = true;
		if (str != null && !"".equals(str.trim())) {
			flag = false;
		}
		return flag;
	}

	public static boolean isNotNullOrEmpty(String str) {
		return !isNullOrEmpty(str);
	}

	public static boolean isNickname(final String str) {
		try {
			byte[] bytes = str.getBytes("gbk");
			if (bytes.length > 20) {
				return false;
			}
		} catch (UnsupportedEncodingException e) {
		}
		final String regex = "[\\u4e00-\\u9fa5\\w]{1,}";
		return match(regex, str);
	}

	public static boolean isEmail(final String str) {
		final String regex = "^[a-zA-Z0-9]{1,}[a-zA-Z0-9\\_\\.\\-]{0,}@(([a-zA-Z0-9]){1,}\\.){1,3}[a-zA-Z0-9]{0,}[a-zA-Z]{1,}$";
		return match(regex, str);
	}

	public static boolean isRegUserName(final String str) {
		final String regex = "[0-9a-zA-Z\\_]{5,20}";
		return match(regex, str);
	}

	public static boolean isLoginUserName(final String str) {
		final String regex = "[0-9a-zA-Z\\_]{3,20}";
		return match(regex, str);
	}

	public static boolean isPassword(final String str) {
		final String regex = "[\\S]{6,15}";
		return match(regex, str);
	}

	private static boolean match(final String regex, final String str) {
		final Pattern pattern = Pattern.compile(regex);
		final Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}

	/**
	 * 格式化时间
	 * 
	 * @param time
	 * @return
	 */
	public static String getFormatTime(long time) {
		SimpleDateFormat format = new SimpleDateFormat("M月d日 HH:mm");
		return format.format(new Date(time));
	}

	public static String getFormatTimeBeforeNow(long time) {

		Calendar currentCalendar = Calendar.getInstance();
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(time);
		int intervalMonths = 0;
		for (int i = 1; i <= 3; i++) {
			currentCalendar.add(Calendar.MONTH, -1);
			if (calendar.before(currentCalendar)) {
				intervalMonths = i;
			} else {
				break;
			}
		}
		currentCalendar = Calendar.getInstance();
		currentCalendar.add(Calendar.MONTH, -intervalMonths);

		long timeInterval = currentCalendar.getTimeInMillis()
				- calendar.getTimeInMillis();

		final long second = 1000;
		final long minute = second * 60;
		final long hour = minute * 60;
		final long day = hour * 24;

		int intervalDays = (int) (timeInterval / day);
		timeInterval -= intervalDays * day;
		int intervalHours = (int) (timeInterval / hour);
		timeInterval -= intervalHours * hour;
		int intervalMinutes = (int) (timeInterval / minute);
		timeInterval -= intervalMinutes * minute;
		// int intervalSeconds = (int) (timeInterval / second);

		if (intervalDays < 2) {
			SimpleDateFormat format = new SimpleDateFormat("a HH:mm");
			return format.format(new Date(time));
		} else if (2 <= intervalDays && intervalDays <= 7) {
			SimpleDateFormat format = new SimpleDateFormat("a E");
			return format.format(new Date(time));
		} else {
			SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
			return format.format(new Date(time));
		}
	}

	/*
	 * public static String getFormatTimeBeforeNow(final long time, final
	 * Context context) { String returnStr = ""; Calendar currentCalendar =
	 * Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
	 * calendar.setTimeInMillis(time);
	 * 
	 * int intervalMonths = 0; for (int i = 1; i <=3; i++) {
	 * currentCalendar.add(Calendar.MONTH, -1); if
	 * (calendar.before(currentCalendar)) { intervalMonths = i; } else { break;
	 * } } currentCalendar = Calendar.getInstance();
	 * currentCalendar.add(Calendar.MONTH, -intervalMonths);
	 * 
	 * if (intervalMonths >= 3) { SimpleDateFormat dateFormat = new
	 * SimpleDateFormat("yyyy-MM-dd"); return dateFormat.format(new
	 * Date(calendar.getTimeInMillis())); }
	 * 
	 * long timeInterval = currentCalendar.getTimeInMillis() -
	 * calendar.getTimeInMillis();
	 * 
	 * final long second = 1000; final long minute = second * 60; final long
	 * hour = minute * 60; final long day = hour * 24;
	 * 
	 * int intervalDays = (int) (timeInterval / day); timeInterval -=
	 * intervalDays * day; int intervalHours = (int) (timeInterval / hour);
	 * timeInterval -= intervalHours * hour; int intervalMinutes = (int)
	 * (timeInterval / minute); timeInterval -= intervalMinutes * minute; int
	 * intervalSeconds = (int) (timeInterval / second);
	 * 
	 * final String[] timeUnits =
	 * context.getResources().getStringArray(R.array.timeUnits); String
	 * str_before = context.getString(R.string.str_before); String str_lt =
	 * context.getString(R.string.str_lt);
	 * 
	 * if (intervalMonths > 0) { returnStr += intervalMonths + timeUnits[0]; if
	 * (intervalDays > 0) { returnStr += intervalDays + timeUnits[1]; }
	 * returnStr += str_before; } else if (intervalDays > 0) { returnStr +=
	 * intervalDays + timeUnits[1]; if (intervalHours > 0) { returnStr +=
	 * intervalHours + timeUnits[2]; } returnStr += str_before; } else if
	 * (intervalHours > 0) { returnStr += intervalHours + timeUnits[2]; if
	 * (intervalMinutes > 0) { returnStr += intervalMinutes + timeUnits[3]; }
	 * returnStr += str_before; } else if (intervalMinutes > 0) { returnStr +=
	 * intervalMinutes + timeUnits[3]; if (intervalSeconds > 0) { // returnStr
	 * += intervalSeconds + timeUnits[4]; } returnStr += str_before; } else if
	 * (intervalSeconds > 0) { returnStr += str_lt + 1 + timeUnits[3]; returnStr
	 * += str_before; // returnStr += intervalSeconds + timeUnits[4]; //
	 * returnStr += str_before; } else { returnStr += str_lt + 1 + timeUnits[3];
	 * returnStr += str_before; // returnStr += timeUnits[5]; } return
	 * returnStr; }
	 */
}
